package com.haojiang.mh

import com.clj.fastble.utils.HexUtil
import com.google.gson.Gson
import com.haojiang.mh.service.ble.BleHandle
import com.haojiang.mh.service.entity.DeviceAttr
import com.haojiang.mh.utils.CrcUtil
import org.junit.Assert.assertEquals
import org.junit.Test
import java.util.*

/**
 * Example local unit test, which will execute on the development machine (host).
 *
 * See [testing documentation](http://d.android.com/tools/testing).
 */
class ExampleUnitTest {
    @Test
    fun addition_isCorrect() {
        assertEquals(4, 2 + 2)
    }

    @Test
    fun test_byte2Hex() {
//        val x: Byte = 0xfa.toByte()
//        println("x: ${BleHandle.instance.byteToHex(x)}")

//        val ss = "fa061305b40b0a5c"
//        val bb =HexUtil.hexStringToBytes(ss)
//        BleHandle.instance.dataAnalyze(bb)

//        for (enumT in MsgType.values()) {
//            val tt = BleHandle.instance.byteToHex(enumT.code)
//            println(tt)
//        }

//        println(HexUtil.encodeHexStr(BleHandle.instance.buildUpProtocol(MsgType.UPDATE_FIRMWARE,"01")))

//        val time= System.currentTimeMillis()
//        println(""+time+"   ---    "+(time/1000 ) )
//        println(BleHandle.instance.getCurrTimeHex())

        val pro = HexUtil.hexStringToBytes("130205")
        println("crc: " + byteToHex(CrcUtil.calcCheck(pro, pro.size)))

        val aa = arrayOf("111", "222", "333")
        val bb = arrayOf("aaa", "bbb", "ccc", "ddd")
        System.arraycopy(aa, 0, bb, 0, aa.size)
        println(Arrays.toString(aa))
        println(Arrays.toString(bb))

    }

    private fun byteToHex(b: Byte): String {
        var hex = Integer.toHexString(b.toInt() and 0xFF)
        if (hex.length < 2) {
            hex = "0$hex"
        }
        return hex.toUpperCase()
    }

    @Test
    fun testProtocol(){
        val dataStr = "7b224d4143223a224430424145344530303135422d443042414534453030313541222c224356223a22312e302e39222c224e56223a22312e302e39222c2253444d6f6465223a312c224c49223a302c225249223a317d"
        val data = HexUtil.hexStringToBytes(dataStr)
//        val content = data.copyOfRange(4, data.size)
        val ss = String(data,Charsets.UTF_8)
        println(ss)
        val g = Gson().fromJson(ss, DeviceAttr::class.javaObjectType)
        println(g.toString())
    }
}
